package com.tmc.service.intair;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.dao.impl.PrivateFareDaoImpl;
import com.tmc.entity.intair.PrivateFare;
import com.tmc.util.DateUtil;
import com.tmc.util.ObjectUtil;
import com.tmc.util.PolicyUtil;
import com.tmc.vendor.asms.bean.FsdBean;
import com.tmc.vendor.asms.bean.FsnBean;
import com.tmc.vendor.asms.util.Asms10000Util;

@Service
@Transactional
public class ToolServiceImpl {

	private Log logger = LogFactory.getLog(ToolServiceImpl.class);

	@Autowired
	private PrivateFareDaoImpl privateFareDao;

	@Autowired
	private PrivateFareServiceImpl privateFareService;

//	private List<FsdBean> filterFsd(List<FsdBean> beans, PrivateFare cond) {
//		List<FsdBean> result = new ArrayList<FsdBean>();
//		for (FsdBean bean : beans) {
//			if (cond.getCabin().indexOf(bean.getCabin()) < 0) {
//				continue;
//			}
//			if (("OW".equals(cond.getFlightType()) && bean.getOwTicketPrice() == null)
//					|| ("RT".equals(cond.getFlightType()) && bean.getRtTicketPrice() == null)) {
//				continue;
//			}
//			/*if (ObjectUtil.hasText(cond.getFcOutboundTravelDate())) {
//				String begin = cond.getFcOutboundTravelDate().split(">")[0];
//				String end = cond.getFcOutboundTravelDate().split(">")[1];
//				if (ObjectUtil.hasText(bean.getTravelDateBegin())
//						&& DateUtil.isBefore(end, bean.getTravelDateBegin())) {
//					continue;
//				}
//				if (ObjectUtil.hasText(bean.getTravelDateEnd()) && DateUtil.isBefore(bean.getTravelDateEnd(), begin)) {
//					continue;
//				}
//			}*/
//			result.add(bean);
//		}
//		return result;
//	}
//
//	private CommonResult checkCond(PrivateFare policy) {
//		if (policy == null) {
//			return CommonResult.makeFail("出票航司不能为空！");
//		}
//		if (ObjectUtil.noText(policy.getTicketingAirline())) {
//			return CommonResult.makeFail("出票航司不能为空！");
//		}
//		if (ObjectUtil.noText(policy.getDepartureArea())) {
//			return CommonResult.makeFail("出发机场不能为空！");
//		}
//		if (ObjectUtil.noText(policy.getArrivalArea())) {
//			return CommonResult.makeFail("到达机场不能为空！");
//		}
//		if (ObjectUtil.noText(policy.getCabin())) {
//			return CommonResult.makeFail("仓位不能为空！");
//		}
//		policy.setCabin(policy.getCabin().trim());
//		if (ObjectUtil.noText(policy.getFlightType())) {
//			return CommonResult.makeFail("行程类型：单程往返不能为空！");
//		}
//		if (policy.getCabin().split("/").length != policy.getTicketPriceStr().split("/").length) {
//			return CommonResult.makeFail("舱位价格不匹配！");
//		}
//		policy.setPriceMap(getPriceMap(policy));
//		return CommonResult.makeSuccess();
//	}
//
//	private Map<String, Double> getPriceMap(PrivateFare policy) {
//		Map<String, Double> map = new HashMap<String, Double>();
//		String[] cabins = policy.getCabin().split("/");
//		String[] prices = policy.getTicketPriceStr().split("/");
//		for (int i=0;i<cabins.length;i++){
//			map.put(cabins[i], Double.valueOf(prices[i]));
//		}
//		return map;
//	}
//
//	@Value("${create_path}")
//	private String create_path;
//
//	public CommonResult privatefareCreate(ContextInfo context, final PrivateFare cond) {
//		CommonResult check = this.checkCond(cond);
//		if (!check.isSuccess()) {
//			return check;
//		}
//		try {
//			List<FsdBean> beans = this.getFsd(context, cond);
//			if (beans == null || beans.isEmpty()) {
//				return CommonResult.makeFail("FSD无匹配运价！ ");
//			}
//			beans = this.getFsn(context, beans, cond);
//			List<PrivateFare> fares = PolicyUtil.buildPrivateFares(beans, cond);
//			if (fares.isEmpty()) {
//				return CommonResult.makeFail("无匹配运价！");
//			}
//			StringBuilder sb = new StringBuilder();
//			for (PrivateFare fare : fares) {
//				ObjectUtil.setBaseInfo(fare, context);
//				privateFareDao.insert(fare);
//				sb.append(fare.getPolicyId()).append(",");
//			}
//			CommonResult result = privateFareService.exportExcel(context, cond.getFlightType(), sb.toString(),
//					create_path);
//			if (result.isSuccess()) {
//				String path = (String) result.getObj();
//				String fileName = path.substring(path.lastIndexOf("\\") + 1);
//				return CommonResult.makeSuccess("成功", fileName);
//			}
//			return privateFareService.exportExcel(context, cond.getFlightType(), sb.toString(), create_path);
//		} catch (Exception e) {
//			logger.error("生成私有运价错误", e);
//			return CommonResult.makeFail("调用黑屏错误！");
//		}
//	}
//
//	private List<FsdBean> getFsd(ContextInfo context, final PrivateFare cond) throws Exception {
//		final String[] cabins = cond.getCabin().split("/");
//		ExecutorService threadPool = Executors.newFixedThreadPool(cabins.length);
//		CompletionService<Entry<String, List<FsdBean>>> completion = new ExecutorCompletionService<Entry<String, List<FsdBean>>>(
//				threadPool);
//		for (final String cabin : cabins) {
//			// 调用shopping接口
//			completion.submit(new Callable<Entry<String, List<FsdBean>>>() {
//				@Override
//				public Entry<String, List<FsdBean>> call() throws Exception {
//					List<FsdBean> beans = AsmsUtil.getFSD(cond.getDepartureArea(), cond.getArrivalArea(),
//							cond.getTicketingAirline(), cond.getFlightType(),
//							DateUtil.toStrDate(new Date(), DateUtil.yyyy_MM_dd), cabin);
//					return new AbstractMap.SimpleEntry<String, List<FsdBean>>(cabin, beans);
//				}
//			});
//		}
//		Map<String, List<FsdBean>> resultMap = getThreadPoolResult(completion, cabins.length);
//		List<FsdBean> beans = ObjectUtil.mapToList1(resultMap);
//		return this.filterFsd(beans, cond);
//	}
//
//	private List<FsdBean> getFsn(ContextInfo context, List<FsdBean> beans, final PrivateFare cond) throws Exception {
//		ExecutorService threadPool = Executors.newFixedThreadPool(beans.size());
//		CompletionService<Entry<String, FsnBean>> completion = new ExecutorCompletionService<Entry<String, FsnBean>>(
//				threadPool);
//		for (final FsdBean bean : beans) {
//			// 调用shopping接口
//			completion.submit(new Callable<Entry<String, FsnBean>>() {
//				@Override
//				public Entry<String, FsnBean> call() throws Exception {
//					FsnBean fsn = AsmsUtil.getFSN(bean, cond);
//					return new AbstractMap.SimpleEntry<String, FsnBean>(bean.getIndex(), fsn);
//				}
//			});
//		}
//		Map<String, FsnBean> resultMap = getThreadPoolResult(completion, beans.size());
//		List<FsdBean> result = new ArrayList<FsdBean>();
//		for (final FsdBean bean : beans) {
//			FsnBean fsn = resultMap.get(bean.getIndex());
//			if (fsn == null){
//				continue;
//			}
//			bean.setFsn(fsn);
//			result.add(bean);
//		}
//		return result;
//	}
//
//	private <T> Map<String, T> getThreadPoolResult(CompletionService<Entry<String, T>> completion, int thread_size)
//			throws Exception {
//		Map<String, T> map = new HashMap<String, T>();
//		for (int i = 0; i < thread_size; i++) {
//			Entry<String, T> entry = completion.take().get();
//			map.put(entry.getKey(), entry.getValue());
//		}
//		return map;
//	}
}
